<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActionController::UrlWriter</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActionController::UrlWriter</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../files/vendor/rails/actionpack/lib/action_controller/url_rewriter_rb.html">
                vendor/rails/actionpack/lib/action_controller/url_rewriter.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Write URLs from arbitrary places in your codebase, such as your mailers.
</p>
<p>
Example:
</p>
<pre>
  class MyMailer
    include ActionController::UrlWriter
    default_url_options[:host] = 'www.basecamphq.com'

    def signup_url(token)
      url_for(:controller =&gt; 'signup', action =&gt; 'index', :token =&gt; token)
    end
 end
</pre>
<p>
In addition to providing <tt><a
href="UrlWriter.html#M000227">url_for</a></tt>, named routes are also
accessible after including <a href="UrlWriter.html">UrlWriter</a>.
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000227">url_for</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000227" class="method-detail">
        <a name="M000227"></a>

        <div class="method-heading">
          <a href="#M000227" class="method-signature">
          <span class="method-name">url_for</span><span class="method-args">(options)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Generate a url based on the options provided, default_url_options and the
routes defined in routes.rb. The following options are supported:
</p>
<ul>
<li><tt>:only_path</tt> If true, the relative url is returned. Defaults to
false.

</li>
<li><tt>:protocol</tt> The protocol to connect to. Defaults to
&#8216;http&#8217;.

</li>
<li><tt>:host</tt> Specifies the host the link should be targetted at. If
<tt>:only_path</tt> is false, this option must be provided either
explicitly, or via default_url_options.

</li>
<li><tt>:port</tt> Optionally specify the port to connect to.

</li>
<li><tt>:anchor</tt> An anchor name to be appended to the path.

</li>
</ul>
<p>
Any other key(:controller, :action, etc&#8230;) given to <tt><a
href="UrlWriter.html#M000227">url_for</a></tt> is forwarded to the Routes
module.
</p>
<p>
Examples:
</p>
<pre>
   url_for :controller =&gt; 'tasks', :action =&gt; 'testing', :host=&gt;'somehost.org', :port=&gt;'8080'    # =&gt; 'http://somehost.org:8080/tasks/testing'
   url_for :controller =&gt; 'tasks', :action =&gt; 'testing', :host=&gt;'somehost.org', :anchor =&gt; 'ok', :only_path =&gt; true    # =&gt; '/tasks/testing#ok'
   url_for :controller =&gt; 'tasks', :action =&gt; 'testing', :host=&gt;'somehost.org', :number =&gt; '33'  # =&gt; 'http://somehost.org/tasks/testing?number=33'
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000227-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000227-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_controller/url_rewriter.rb, line 47</span>
47:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">url_for</span>(<span class="ruby-identifier">options</span>)
48:       <span class="ruby-identifier">options</span> = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">default_url_options</span>.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">options</span>)
49:       
50:       <span class="ruby-identifier">url</span> = <span class="ruby-value str">''</span>
51: 
52:       <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span> <span class="ruby-identifier">:only_path</span>
53:         <span class="ruby-identifier">url</span> <span class="ruby-operator">&lt;&lt;</span> (<span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:protocol</span>) <span class="ruby-operator">||</span> <span class="ruby-value str">'http'</span>)
54:         <span class="ruby-identifier">url</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-value str">'://'</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">url</span>.<span class="ruby-identifier">match</span>(<span class="ruby-value str">&quot;://&quot;</span>) <span class="ruby-comment cmt">#dont add separator if its already been specified in :protocol </span>
55:         
56:         <span class="ruby-identifier">raise</span> <span class="ruby-value str">&quot;Missing host to link to! Please provide :host parameter or set default_url_options[:host]&quot;</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:host</span>]
57: 
58:         <span class="ruby-identifier">url</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:host</span>)
59:         <span class="ruby-identifier">url</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-node">&quot;:#{options.delete(:port)}&quot;</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">key?</span>(<span class="ruby-identifier">:port</span>)
60:       <span class="ruby-keyword kw">else</span>
61:         <span class="ruby-comment cmt"># Delete the unused options to prevent their appearance in the query string</span>
62:         [<span class="ruby-identifier">:protocol</span>, <span class="ruby-identifier">:host</span>, <span class="ruby-identifier">:port</span>].<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">k</span><span class="ruby-operator">|</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span> <span class="ruby-identifier">k</span> }
63:       <span class="ruby-keyword kw">end</span>
64: 
65:       <span class="ruby-identifier">anchor</span> = <span class="ruby-node">&quot;##{CGI.escape options.delete(:anchor).to_param.to_s}&quot;</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">key?</span>(<span class="ruby-identifier">:anchor</span>)
66:       <span class="ruby-identifier">url</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">Routing</span><span class="ruby-operator">::</span><span class="ruby-constant">Routes</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">options</span>, {})
67:       <span class="ruby-identifier">url</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">anchor</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">anchor</span>
68: 
69:       <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">url</span>
70:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>